import os
import time

import mindspore
from mindspore import context
import ipywidgets as wgs

# dropdown = wgs.Dropdown(description='Select Devices', options=['Click to choose',
#                                                                "CPU", 'Ascend NPU'])
# output = wgs.Output()
#
#
# def changeDevice(change):
#     output.clear_output()
#     token = change.new
#     with output:
#         if token == 'Click to choose':
#             print("请选择环境")
#         if token == 'CPU':
#             print('请确认当前环境为CPU\n===============')
#             try:
#                 context.set_context(mode=context.GRAPH_MODE, device_target='CPU', save_graphs=False)
#                 print('已检查！确认当前环境为CPU')
#             except ValueError:
#                 print('错误！当前系统硬件不是CPU,请重新选择合适的硬件设备')
#
#             if token == "Ascend NPU":
#                 try:
#                     context.set_context(mode=context.GRAPH_MODE, device_target='Ascend', save_graphs=False)
#                     print('已检查！确认当前环境为 Ascend NPU')
#                 except ValueError:
#                     print('错误！当前系统硬件不是Ascend NPU,请重新选择合适的硬件设备')
#
#
# dropdown.observe(changeDevice, names='value')
# display(dropdown, output)

epoch_size = 1
batch_size = 2
pre_trained = True
dataset_sink_mode = False
save_ckpt_num = 1
per_print_steps = 200

import os
import requests
import time
import zipfile


def download_and_unzip(url, path):
    if not os.path.exists(path):
        os.mkdir(path)
    file_path = os.path.join(path, 'case6.zip')
    start = time.time()
    user, password = 'tyx_neu', 'Sportlab307'
    resp = requests.get(url, auth=(user, password), stream=True)
    size = 0
    chunk_size = 1024
    content_size = int(resp.headers['content-length'])
    try:
        if resp.status_code == 200:
            print(f'start download,[File size]:{size:.2f} MB'.format(size=content_size / chunk_size / 1024))
            with open(file_path, 'wb') as f:
                for data in resp.iter_content(chunk_size=chunk_size):
                    f.write(data)
                    size += len(data)
                    print('\r' + '[下载进度]:%s%.2f%%' % (
                        '>' * int(size * 50 / content_size), float(size / content_size * 100)), end=' ')
        end = time.time()
        print('\n' + 'download completed!,times: %.2秒' % (end - start))
    except ValueError:
        print("error!")
    unzip_file_path = os.path.join(path, 'flowers')
    if not os.path.exists(unzip_file_path):
        os.mkdir(unzip_file_path)
    zip_file = zipfile.ZipFile(file_path)
    zip_list = zip_file.namelist()
    for f in zip_list:
        zip_file.extract(f, unzip_file_path)
    zip_file.close()
    print("successfully unzip download flower dataset from websit")


url = 'https://openi.pcl.ac.cn/attachments/03327b86-51f3-47fe-820c-b79a761ba083?type=1'
download_and_unzip(url, os.path.join(os.getcwd(), 'dataset'))
